/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wso.global;

import java.io.Serializable;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Violet_Ink
 */
public class BaseDao extends HibernateDaoSupport{

    public Object get(Class cls, Serializable id) {
        Object result = null;
        try {
            result = getHibernateTemplate().get(cls, id);
        } catch (DataAccessException ex) {
            throw ex;
        }
        return result;

    }

    public List<Object> getAll(Class cls) {
        List<Object> resultList = null;
        try {
            resultList = getHibernateTemplate().loadAll(cls);
        } catch (DataAccessException ex) {
            throw ex;
        }
        return resultList;
    }

    public void delete(Class cls, Serializable id) {
        Object dto = get(cls, id);
        try {
            if (dto != null) {
                getHibernateTemplate().delete(dto);
            } else {
                return;
            }
        } catch (DataAccessException ex) {
            throw ex;
        }
    }

    public void update(Class cls, Serializable id) {
        Object dto = get(cls, id);
        try {
            if (dto != null) {
                getHibernateTemplate().update(dto);
            } else {
                return;
            }
        } catch (DataAccessException ex) {
            throw ex;
        }
    }

    public void save(Object dto) {
        try {
            if (dto != null) {
                getHibernateTemplate().save(dto);
            } else {
                return;
            }
        } catch (DataAccessException ex) {
            throw ex;
        }
    }

    public void update(Object dto) {
        try {
            if (dto != null) {
                getHibernateTemplate().update(dto);
            } else {
                return;
            }
        } catch (DataAccessException ex) {
            throw ex;
        }
    }

    public void saveOrUpdate(Object dto) {
        try {
            if (dto != null) {
                getHibernateTemplate().saveOrUpdate(dto);
            } else {
                return;
            }
        } catch (DataAccessException ex) {
            throw ex;
        }

    }

    public void flush() {
        try {
            getHibernateTemplate().flush();
        } catch (DataAccessException ex) {
            throw ex;
        }
    }

    public List find(String queryString, Object[] values) {
        List resultList = null;
        try {
            resultList = getHibernateTemplate().find(queryString, values);
        } catch (DataAccessException ex) {
            throw ex;
        }
        return resultList;
    }

    public List find(String queryString, String[] params, Object[] values) {
        List resultList = null;
        try {
            resultList = getHibernateTemplate().findByNamedParam(queryString, params, values);
        } catch (DataAccessException ex) {
            throw ex;
        }
        return resultList;
    }

    public Object getMaxId(String primarykey, String className) {
        List result = null;
        try {
            StringBuilder str = new StringBuilder();
            str.append("select max(").append(primarykey).append(") from ").append(className);

            result = getHibernateTemplate().find(str.toString());
        } catch (DataAccessException e) {
            throw e;
        }
        return (Integer) result.get(0);
    }
}
